#include "Common.hpp"

// ./client_tcp server_ip server_port
int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        cout << "Usage:./client_tcp server_ip server_port" << endl;
        return 1;
    }
    string server_ip = argv[1];  // "192.161.1.1"  点分十进制
    uint16_t server_port = stoi(argv[2]);
    int sockfd = ::socket(AF_INET, SOCK_STREAM, 0);
    if(sockfd < 0)
    {
        cout << "socket create failed" << endl;
        return 2;
    }
    struct sockaddr_in server_addr;
    bzero(&server_addr, sizeof(server_addr));
    server_addr.sin_port = htons(server_port);
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(server_ip.c_str());

    // connect
    // client 是不需要显示的进行bind的，tcp是面向连接的，connect底层会自动进行bind
    int n = ::connect(sockfd, CONV(&server_addr), sizeof(server_addr));
    if(n < 0)
    {
        cout << "connect failed" << endl;
        return 3;
    }
    // echo client 发送消息
    string message;  // 要发送的消息
    while(true)
    {
        char inbuffer[1024];  // 接受消息的数组
        cout << "input message: ";
        getline(cin, message);
        n = ::write(sockfd, message.c_str(), message.size());  // 往sockfd的文件中写入message
        if(n > 0)  // 有消息
        {
            int m = ::read(sockfd, inbuffer, sizeof(inbuffer));  // 将从sockfd文件中读到的消息放入inbuffer
            if(m > 0)  // 读成功
            {
                inbuffer[m] = '\0';
                cout << inbuffer << endl;
            }
            else break;
        }
        else break;
    }
    ::close(sockfd);
    return 0;
}